package cn.assumejson.springframework.context.context.annotation;

import cn.assumejson.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import cn.assumejson.springframework.beans.factory.config.BeanDefinitionCustomizer;
import cn.assumejson.springframework.beans.factory.support.BeanDefinitionRegistry;
import cn.assumejson.springframework.beans.factory.support.RootBeanDefinition;
import cn.hutool.core.lang.Assert;

import java.beans.Introspector;
import java.lang.annotation.Annotation;
import java.util.function.Supplier;

/**
 * @author Mr. Li
 * @version 1.0
 * @since 1.0 2021/12/5 13:50
 */
public class AnnotatedBeanDefinitionReader {
    private final BeanDefinitionRegistry registry;


    public AnnotatedBeanDefinitionReader(BeanDefinitionRegistry registry) {
        Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
        //把ApplicationContext对象赋值给AnnotatedBeanDefinitionReader
        this.registry = registry;

        //注册一些内置的后置处理器
        AnnotationConfigUtils.registerAnnotationConfigProcessors(this.registry);
    }


    /**
     * Register one or more component classes to be processed.
     * <p>Calls to {@code register} are idempotent; adding the same
     * component class more than once has no additional effect.
     *
     * @param componentClasses one or more component classes,
     *                         e.g. {@link Configuration @Configuration} classes
     */
    public void register(Class<?>... componentClasses) {
        for (Class<?> componentClass : componentClasses) {
            registerBean(componentClass);
        }
    }

    /**
     * Register a bean from the given bean class, deriving its metadata from
     * class-declared annotations.
     *
     * @param beanClass the class of the bean
     */
    public void registerBean(Class<?> beanClass) {
        doRegisterBean(beanClass, null, null, null, null);
    }

    private <T> void doRegisterBean(Class<T> beanClass, String name,
                                    Class<? extends Annotation>[] qualifiers, Supplier<T> supplier,
                                    BeanDefinitionCustomizer[] customizers) {

        // 包装为AnnotaoGenner类型
//        AnnotatedGenericBeanDefinition annotatedGenericBeanDefinition = new AnnotatedGenericBeanDefinition(beanClass);

        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition(beanClass);

        // TODO 解析启动类是否包含 @Lazy、@Primary、@DependsOn、@Role、@Description
        this.registry.registerBeanDefinition(Introspector.decapitalize(beanClass.getSimpleName()), rootBeanDefinition);

        //存储@Configuration注解注释的类
//        Configuration annotation = beanClass.getAnnotation(Configuration.class);
//        if (annotation != null) {
//            RootBeanDefinition rbd = new RootBeanDefinition(beanClass);
//
////            this.registry(Introspector.decapitalize(Configuration.class.getName()), rbd);
//        }

    }

    public BeanDefinitionRegistry getRegistry() {
        return registry;
    }
}
